package com.example.springMVC.controller;

import com.example.springMVC.commons.Result;
import com.example.springMVC.commons.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;

@Controller
public class FileController {


    @ResponseBody
    @RequestMapping("upload")
    public Result upload(MultipartFile file) throws IOException, ServiceException {
        //获取文件名称  a.jpg
        String fileName = file.getOriginalFilename();
        //文件存放路径
        String realPath = "C://Users//34321//GitHub//springMVC//src//main//webapp//statics//images//";
        File f1 = new File(realPath);
        if (!f1.exists()) {
            boolean b = f1.mkdir();
            if (!b) {
                throw new ServiceException("创建文件失败");
            }
        }

        //防止文件名重复 修改文件名
        String uuidName = UUID.randomUUID().toString();
        String newFile = uuidName + fileName.substring(fileName.lastIndexOf("."));
//        if (!".png".equalsIgnoreCase(fileName.substring(fileName.lastIndexOf(".")))
//        && !".jpg".equalsIgnoreCase(fileName.substring(fileName.lastIndexOf(".")))) {
//            throw new ServiceException("请放入正确的图片");
//        }
        if(!StringUtils.equalsAnyIgnoreCase(fileName.substring(fileName.lastIndexOf(".")),".png",".jpg")){
            throw new ServiceException("请放入正确的图片");
        }

        File f = new File(realPath + newFile);
        file.transferTo(f);
        return Result.success(newFile);
    }
    @RequestMapping("downLoad")
    public void downLoad(String fileName, HttpServletResponse response) throws IOException, ServiceException {
        //文件真实路径
        String path = "C://Users//34321//GitHub//springMVC//src//main//webapp//statics//images//"+fileName;
        //new一个File
        File file = new File(path);
        //判断文件是否存在
        if(!file.exists()){
            throw new ServiceException("文件不存在");
        }
        //设置文件名utf-8
        fileName = URLEncoder.encode(fileName,"UTF-8");
        //设置下载头
        response.addHeader("Content-Disposition","attachment;filename="+fileName);
        //自动判断下载文件类型
        response.setContentType("multipart/form-data");

        //读取下载文件 用Buffer缓冲流加快速度
        InputStream is = new BufferedInputStream(new FileInputStream(path));

        //创建输出流
        ServletOutputStream out = response.getOutputStream();

        //每次循环读取多少字节
        byte[] buffer = new byte[1024];
        //行
        int len;
        //循环输出字节
        while ((len = is.read(buffer)) != -1){
            out.write(buffer,0,len);
        }
        //关闭流
        is.close();
        out.flush();
        out.close();
    }
}
